import myData from "./myData";

export default function wxLoad(){
  const ws = new WebSocket("ws://127.0.0.1:9914");
  window.ws = ws;
  
  ws.onopen = function () {
    //当WebSocket创建成功时，触发onopen事件
    console.log("open");
  }
  
  ws.onmessage = function(e){
    let res = JSON.parse(e.data)
    console.log('onmessage',res); 
    init(res)
    //保存数据
    myData.there.saveData(res.data);myData.there.saveSheet(res.sheet);
  }

  function init(res){
    const xs = x_spreadsheet('#x-spreadsheet-demo', {
      showToolbar: true,
      showGrid: true,
      showBottomBar: true,
      extendToolbar: {
        left: [
          {
            tip: 'Save',
            icon: saveIcon,
            onClick: (data, sheet) => {
              console.log('click save button：', data, sheet)
            }
          }
        ],
        right: [
          {
            tip: 'Preview',
            el: previewEl,
            onClick: (data, sheet) => {
              console.log('click preview button：', data)
            }
          }
        ],
      }
    },res)

    xs.on('cell-selected', (cell, ri, ci) => {
      console.log('cell:', cell, ', ri:', ri, ', ci:', ci);
    }).on('cell-edited', (text, ri, ci) => {
      console.log('text:', text, ', ri: ', ri, ', ci:', ci);
    });
  }

  var saveIcon = ''
  var previewEl = document.createElement('img')
  previewEl.src = ''
  previewEl.width = 16
  previewEl.height = 16
}

